package com.asset.web.purchase;

import com.asset.pojo.Category;
import com.asset.service.CategoryService;
import com.asset.util.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 11725
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 分页查询 | 查询全部信息
     * @param page          页码
     * @param rows          行数
     * @param selectAll     是否查询全部
     * @return              结果集
     */
    @GetMapping("/list")
    public ResponseEntity<PageResult<Category>> selectByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "10") Integer rows,
            @RequestParam(value = "selectAll", required = false) Boolean selectAll) {
        return ResponseEntity.ok(categoryService.selectByPage(page, rows, selectAll));
    }

    /**
     * 根据 id 查询分类信息
     * @param id        分类 id
     * @return          Category 对象
     */
    @GetMapping("/{id}")
    public ResponseEntity<Category> selectById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(categoryService.selectById(id));
    }

    @GetMapping
    public ResponseEntity<List<Category>> selectByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(categoryService.selectByIds(ids));
    }

    @PostMapping
    public ResponseEntity<Void> insert(@RequestBody Category category) {
        categoryService.insert(category);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> update(@RequestBody Category category) {
        categoryService.update(category);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id) {
        categoryService.delete(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
